#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <utility>
#include <functional>
#include <bitset>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define all(v) v.begin(),v.end()
using namespace std;
ll max(ll a ,ll b);
ll min(ll a ,ll b);
ll gcd(ll a, ll b);
ll lcm(ll a, ll b);
const int mod = 1000000007;
void solve() {
int n;cin >> n;
vector<pii> v;
ll a,b,last,x;
for(int i = 0;i<n;++i) {
cin >> a >> b;
v.pb({a,b});
}
sort(all(v));
ll c = 1,ans = 0;
ll took = 0;
for(int i = 0;i<n;++i) {
c = v[i].first;
took = 0;
vector<pair<ll,ll>> temp;
while(i<n && v[i].first==c) {
temp.push_back({v[i].second,v[i].first});
i++;
}
i--;
sort(temp.rbegin(),temp.rend());
int j = 0;
x = temp.size();
for(j = 0;j<x && j<c-took;++j) {
ans+=(temp[j].first);
}
}
cout << ans << endl;
}
int main(){
std::ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--)
{
solve();
}
return 0;
}
ll max(ll a,ll b){
return (a>b)?a:b;
}
ll min(ll a ,ll b){
return (a<b)?a:b;
}
ll gcd(ll a ,ll b){
if(b==0) return a;
return gcd(b,a%b);
}
ll lcm(ll a ,ll b){
return (a*b)/gcd(a,b);
}
Going to office | Color the boxes |
Missing numbers | Maximum sum |
13 Reasons Why | Friend's Relationship |
Health of a person | Divisibility |
A. Movement | Numbers in a matrix |
Sequences | Split houses |
Divisible | Three primes |
Coprimes | Cost of balloons |
One String No Trouble | Help Jarvis! |
Lift queries | Goki and his breakup |
Ali and Helping innocent people | Book of Potion making |
Duration | Birthday Party |
e-maze-in | Bricks Game |
Char Sum | Two Strings |
Anagrams | Prime Number |